package com.lly.songhigh.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lly.songhigh.R;
import com.lly.songhigh.activity.MainActivity;
import com.lly.songhigh.adapter.DiscoverFragmentAdapter;

import java.util.ArrayList;

/**
 * Created by dllo on 15/10/20.
 */
public class DiscoverFragment extends BaseFragment {

    private DiscoverFragmentAdapter adapter;
    private TabLayout discoverTablayout;
    private ViewPager discoverViewpager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        discoverTablayout = (TabLayout) view.findViewById(R.id.discover_tablayout);
        discoverViewpager = (ViewPager) view.findViewById(R.id.discover_viewpager);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new DiscoverFragmentAdapter(getActivity().getSupportFragmentManager());
        discoverViewpager.setAdapter(adapter);
        discoverTablayout.setupWithViewPager(discoverViewpager);
        discoverTablayout.setSelectedTabIndicatorColor(0xffc03c35);
        discoverTablayout.setTabTextColors(0xff515151, 0xffc03c35);
    }


    @Override
    public void onDestroyView() {
        if (!((MainActivity) getActivity()).isDestory) {
            ArrayList<Fragment> fragments = adapter.getFragments();
            for (Fragment fragment : fragments) {
                getFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
            }
        }
        super.onDestroyView();
    }
}
